import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import '../dd_models/dd_oos_model.dart';
import 'dd_other.dart';

const String host_url = 'http://test-app.bigegg.work/';

const String log_url = 'https://log.bigegg.work/log/live-chat';

const String privacyPolicyUrl =
    'https://h5.cosmofusionly.xyz/privacyPolicy.html';

const String termConditionsUrl =
    'https://h5.cosmofusionly.xyz/termConditions.html';

class DDHttpUrl {
  static String getAppConfig = 'config/getAppConfig';

  static String getStrategy = 'config/getStrategy';

  static String rongcloudToken = 'user/rongcloud/token';

  static String oauth = 'security/oauth';

  static String isValidToken = 'security/isValidToken';

  static String logout = 'security/logout';

  static String deleteAccount = 'user/deleteAccount';

  static String getUserInfo = 'user/getUserInfo';

  static String saveUserInfo = 'user/saveUserInfo';

  static String wallSearch = 'broadcaster/wall/search';

  static String mediaSearch = 'shortLink/media/search';

  static String goodsSearch = 'coin/goods/search';

  static String rechargeCreate = 'coin/recharge/create';

  static String paymentIpa = 'coin/recharge/payment/ipa';

  static String reviewModeConsume = 'coin/reviewModeConsume';

  static String getUserCoins = 'user/getUserCoins';

  static String ossPolicy = 'user/oss/policy';

  static String updateAvatar = 'user/updateAvatar';

  static String addFriend = 'user/addFriend';

  static String unfriend = 'user/unfriend';

  static String insertRecord = 'report/complain/insertRecord';

  static String removeBlock = 'report/complain/removeBlock';

  static String blockList = 'report/complain/blockList';

  static String getFriendsListPage = 'user/getFriendsListPage';
}

final dio = Dio();

Future<Map<String, dynamic>> getHeaders(
    {bool auth = true, bool json = true}) async {
  Map<String, dynamic> headers = {};
  headers['Accept'] = '*/*';
  if (json) {
    headers['Content-Type'] = 'application/json';
  } else {
    headers['Content-Type'] = 'application/x-www-form-urlencoded';
  }
  headers['device-id'] = DDAppInfo.getInstance().udid;
  headers['platform'] = DDAppInfo.getInstance().systemName;
  headers['model'] = DDAppInfo.getInstance().machine;
  headers['pkg'] = DDAppInfo.getInstance().packageName;
  //headers['pkg'] = "test.cusmofusion.ios";
  headers['ver'] = DDAppInfo.getInstance().version;
  headers['p_ver'] = DDAppInfo.getInstance().buildNumber;
  headers['kst'] = '1';
  headers['sys_lan'] = DDAppInfo.getInstance().localLanguageCode;
  headers['lang'] = DDAppInfo.getInstance().localLanguageCode;
  if (auth) {
    final authorizationToken =
        await DDAppInfo.getInstance().getAuthorizationToken();
    if (authorizationToken != null) {
      headers['Authorization'] = 'Bearer${authorizationToken}';
    }
  }
  return headers;
}

Future<Map<String, dynamic>?> getAppConfig() async {
  var headers = await getHeaders(auth: false);
  dio.options.headers = headers;

  Map<String, dynamic> queryParams = {
    'ver': 0,
  };

  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.getAppConfig,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> oauth(String identity, int oauthType) async {
  var headers = await getHeaders(auth: false, json: false);
  dio.options.headers = headers;

  Map<String, dynamic> queryParams = {
    'oauthType': oauthType,
    'token': identity,
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.oauth,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> rongCloudToken() async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.rongcloudToken,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> isValidToken() async {
  var headers = await getHeaders(auth: false);
  dio.options.headers = headers;

  final authorizationToken =
      await DDAppInfo.getInstance().getAuthorizationToken();

  Map<String, dynamic> queryParams = {
    'token': authorizationToken,
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.isValidToken,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> getStrategy() async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.getStrategy,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> securityLogout() async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.logout,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> userDeleteAccount() async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.deleteAccount,
    );
    Map<String, dynamic> jsonResponse = response.data;
    debugPrint('Request path: ${response.requestOptions.path}');
    debugPrint('Reques headerst: ${response.requestOptions.headers}');
    debugPrint('Reques data: ${response.requestOptions.data}');
    debugPrint('Response data: ${response.data}');
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> wallSearch(
    {bool isPageMode = true, int limit = 20, int page = 1}) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {
    "category": "Popular",
    "isPageMode": isPageMode,
    "isRemoteImageUrl": true,
    "limit": limit,
    "page": page,
    "tag": "All",
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.wallSearch,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> getUserInfo(String userId) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {
    "userId": userId,
  };

  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.getUserInfo,
     // data: queryParams,
    );
    debugPrint('Request path: ${response.requestOptions.path}');
    debugPrint('Reques headerst: ${response.requestOptions.headers}');
    debugPrint('Reques data: ${response.requestOptions.data}');
    debugPrint('Response data: ${response.data}');
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> saveUserInfo(
    String nickname, String birthday, String country) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {
    "birthday": birthday,
    "country": country,
    "nickname": nickname,
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.saveUserInfo,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> mediaSearch() async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = [
    {"mediaPath": "SabreSync/video/1.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/2.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/3.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/4.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/5.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/6.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/7.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/8.mp4", "mediaType": "video"},
    {"mediaPath": "SabreSync/video/9.mp4", "mediaType": "video"}
  ];

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.mediaSearch,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> goodsSearch() async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {"isIncludeSubscription": false, "payChannel": "IAP"};

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.goodsSearch,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> rechargeCreate(
    String goodsCode, String payChannel) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {
    "entry": '',
    "source": '',
    "goodsCode": goodsCode,
    "payChannel": payChannel,
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.rechargeCreate,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> paymentIpa(
    String orderNo, String payload, String transactionId,
    {String type = "1"}) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var queryParams = {
    "orderNo": orderNo,
    "payload": payload,
    "transactionId": transactionId,
    "type": type,
  };

  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.paymentIpa,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> reviewModeConsume(int outlay) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  var queryParams = {
    "outlay": outlay,
    "source": "iap",
  };
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.reviewModeConsume,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> getUserCoins() async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.getUserCoins,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}


Future<Map<String, dynamic>?> addFriend(String followUserId) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  var queryParams = {
    "followUserId": followUserId,
  };
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.addFriend,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> unfriend(String followUserId) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  var queryParams = {
    "followUserId": followUserId,
  };
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.unfriend,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> insertRecord(
    Map<String, dynamic> queryParams) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.insertRecord,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> removeBlock(String BlockUserId) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  var queryParams = {
    "blockUserId": BlockUserId,
  };
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.removeBlock,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}



Future<Map<String, dynamic>?> blockList(int limit,int page) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.blockList,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}


Future<Map<String, dynamic>?> getFriendsListPage(int limit,int page) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  var queryParams = {
    "limit": limit,
    "page": page,
  };
  try {
    Response response = await dio.post(
      host_url + DDHttpUrl.getFriendsListPage,
      data: queryParams,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}



Future<Map<String, dynamic>?> ossPolicy() async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response = await dio.get(
      host_url + DDHttpUrl.ossPolicy,
    );
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

Future<Map<String, dynamic>?> uploadImageOss(
    DDOssModel model, String imagePath) async {
  String fileName = imagePath.split('/').last;
  DateTime dateTime = DateTime.now();
  String format = fileName.split('.').last;
  int imageTimeName = dateTime.millisecondsSinceEpoch +
      (dateTime.microsecondsSinceEpoch ~/ 1000000);
  String imageName = '$imageTimeName.$format';
  String host = model.host;
  String dir = model.dir;

  var filePath = await MultipartFile.fromFile(imagePath, filename: fileName);
  final formData = FormData.fromMap({
    'ossaccessKeyId': model.accessKeyId,
    'policy': model.policy,
    'signature': model.signature,
    'callback': model.callback,
    'key': '$dir$imageName',
    'file': filePath,
  });
  try {
    Response response = await dio.post(host, data: formData);
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}


Future<Map<String, dynamic>?> updateAvatar(String imageUrl) async {
  var headers = await getHeaders();
  dio.options.headers = headers;

  var avatarPath = {
    'avatarPath': imageUrl,
  };

  try {
    Response response =
        await dio.post(host_url + DDHttpUrl.updateAvatar, data: avatarPath);
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}


Future<Map<String, dynamic>?> sendLogEvent(List<Map<String, dynamic>> param) async {
  var headers = await getHeaders();
  dio.options.headers = headers;
  try {
    Response response =
    await dio.post(log_url, data: param);
    Map<String, dynamic> jsonResponse = response.data;
    return jsonResponse;
  } catch (error) {
    return null;
  }
}

